/*
 *	Copyright (c) 2013, Yulong Information Technologies
 *	All rights reserved.
 *  
 *  @Project: Db
 *  @author: Robot
 *	@email: feng88724@126.com
 */
package com.example.db.core;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.db.core.annotation.PK;
import com.example.db.core.annotation.Table;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

/**
 * @author Robot
 * @weibo http://weibo.com/feng88724
 * @date Apr 10, 2013
 */
public class Model<T extends Model> implements Serializable {
	private static final String TAG = "DB";
	
	/**
	 * 获取父类泛型类型
	 * @return
	 */
	private Class<?> getTableClass() {
//		Type type = getClass().getGenericSuperclass();
//		Class<?> clazz = null;
//		if (type instanceof ParameterizedType) {
//			clazz = (Class<?>)((ParameterizedType)type).getActualTypeArguments()[0];
//		}
//		return clazz;
		return getClass();
	}
	
	/**
	 * 获取数据表名称
	 * @return
	 */
	private String getTableName() {
		return DBKit.getTableName(getTableClass());
	}
	
	/**
	 * 插入数据
	 * @return
	 */
	public long save() {
		return DBKit.save(this);
	}
	
	private Cursor select(String sql, String[] bindArgs) {
		return DBConfig.getDatabase().rawQuery(sql, bindArgs);
	}
	
	/**
	 * 查询, 只返回一条满足条件的记录
	 * @param sql
	 * @param bindArgs
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T find(String sql, String[] bindArgs){
		Cursor cursor = select(sql, bindArgs);
		T t = null;
		if(cursor.moveToFirst()){
			try {
				t = (T)getTableClass().newInstance();

				//把列的值，转换成对象里属性的值
				DBKit.columnToField(t, cursor);
			} catch(Exception e) {
				Log.e(TAG, "", e);
			}
		}
		cursor.close();
		return t;
	}
	
	/**
	 * 查询, 返回记录集
	 * @param sql
	 * @param bindArgs
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> findList(String sql, String[] bindArgs) {
		Cursor cursor = this.select(sql, bindArgs);
		List<T> list = new ArrayList<T>(cursor.getCount());
		T t = null;
		while (cursor.moveToNext()) {
			try {
				t = (T)this.getClass().newInstance();

				//把列的值，转换成对象里属性的值
				DBKit.columnToField(t, cursor);

				list.add(t);
			} catch(Exception e) {
				Log.e(TAG, "", e);
			}
		}
		cursor.close();
		return list;
	}

	/**
	 * 获取所有记录
	 * @return
	 */
	public List<T> findAll(){
		return this.findList("select * from " + getTableName(), null);
	}
	
	//----------------------------------------------------------------------------------------
	// 更新
	//----------------------------------------------------------------------------------------
	
	/**
	 * 更新
	 * 根据主键来更新, 必须包含主键及主键值
	 * @return
	 */
	public boolean update() {
		return DBKit.update(this);
	}
	
	/**
	 * 更新
	 * @param values			数据
	 * @param whereClause		条件
	 * @param whereArgs			条件值
	 */
	public int update(ContentValues values, String whereClause, String[] whereArgs) {
		return DBKit.update(getTableName(), values, whereClause, whereArgs);
	}
	
	//----------------------------------------------------------------------------------------
	// 删除
	//----------------------------------------------------------------------------------------
	
	/**
	 * 根据主键删除
	 * @return
	 */
	public boolean delete() {
		return DBKit.delete(this);
	}
	
	/**
	 * 根据其他条件删除
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public boolean delete(String whereClause, String[] whereArgs) {
		return DBKit.delete(getTableName(), whereClause, whereArgs);
	}
}
